package com.jeomo.mem.starter.util;

import com.jeomo.common.core.util.StringUtils;
import com.jeomo.mem.starter.entity.AbstractMemberRule;

import java.util.Map;

/**
 * 会员工具类
 *
 * @author:Jackpot
 * @create 2020-10-27 17:25
 * @description:
 */
public class MemberUtil {

    /**
     * 多值列:从缓存中根据code 取出name
     *
     * @param memberPointsRule 实体
     * @param cacheMap         缓存
     */
    public static void assignName(AbstractMemberRule memberPointsRule, Map<String, String> cacheMap) {
        if (memberPointsRule == null) return;
        //多值的字符串(逗号隔开):v1,v2,v3
        String[] levelCodes = StringUtils.strToArray(memberPointsRule.getUseLevels());
        String[] levelNames = new String[levelCodes.length];
        for (int i = 0; i < levelCodes.length; i++) {
            String levelCode = levelCodes[i];
            String levelName = cacheMap.get(levelCode);
            if (StringUtils.isEmpty(levelName)){//获取name 失败,有可能是原数据被删除,此时返回code;
                levelName = levelCode;
            }
            levelNames[i] = levelName;
        }
        //以数组类型返回多值的编码和名称
        memberPointsRule.setLevelCodes(levelCodes);
        memberPointsRule.setLevelNames(levelNames);
    }

}
